home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8691 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.7 KB  |  85 lines

  1. Path: rcp6.elan.af.mil!rscernix!danpop
  2. From: danpop@mail.cern.ch (Dan Pop)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: microseconds??
  5. Date: 5 Mar 96 12:03:36 GMT
  6. Organization: CERN European Lab for Particle Physics
  7. Message-ID: <danpop.826027416@rscernix>
  8. References: <4h62s9$2o7@chaos.dac.neu.edu> <4h7netINN1hk@anvil.ugrad.cs.ubc.ca>
  9. NNTP-Posting-Host: ues5.cern.ch
  10. X-Newsreader: NN version 6.5.0 #7 (NOV)
  11.  
  12. In <4h7netINN1hk@anvil.ugrad.cs.ubc.ca> c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku) writes:
  13.  
  14. >In article <4h62s9$2o7@chaos.dac.neu.edu>,
  15. >hadi abedi <habedi@lynx.dac.neu.edu> wrote:
  16. >>Hello,
  17. >>    Thanks for taking your time to read this.
  18. >>    
  19. >>    Is there a way to compute microsecond intervals    
  20. >>    with C?
  21. >>    I'd like to time a sorting routine on different
  22. >>    list sizes. I am aware of the 'time' function on
  23. >>    the UNIX system, but I'd like to know how to do 
  24. >>    it in C.
  25. >>    Thanks.
  26. >
  27. >The _best_ way to do this under a particular environment is up to, well, that
  28. >particular environment. The standard C way of timing is the the clock()
  29. >function. Converting to seconds means dividing by the symbolic constant
  30. >CLOCKS_PER_SEC defined in <time.h>. Hopefully, this is a few order of
  31. >magnitudes greater than 1.
  32. >
  33. >On this HP-UX system, the actual value is:
  34. >
  35. >/* ANSI C time constants, types, and structures */
  36. >
  37. >#ifdef _INCLUDE__STDC__
  38. >#  define CLOCKS_PER_SEC 1000000
  39. >
  40. >#  ifndef NULL
  41. >#    define NULL 0
  42. >#  endif
  43. >
  44. >So you see, the clock() function can have microsecond resolution (I'm not
  45. >saying that the hardware does, just the clock() function).
  46.  
  47. The sad truth is that clock() cannot have a better resolution than the
  48. hardware it runs on.
  49.  
  50. >It's likely that it is implemented with the best possible method that yields
  51. >the greatest resolution. The above clearly shows that CLOCKS_PER_SEC does not
  52. >have to be restricted to the preemptive clock frequency of the operating system
  53. >(i.e. the HZ value).
  54.  
  55. The resolution of clock() is however restricted to the CLK_TCK value
  56. (i.e. the value produced by sysconf(_SC_CLK_TCK)).  The value of
  57. CLOCKS_PER_SEC is completely irrelevant to the resolution provided by
  58. clock(), even if you have to use it to convert to seconds.  Proof:
  59.  
  60.     hpplus06:~/tmp 8> uname -a
  61.     HP-UX hpplus06 A.09.05 A 9000/735 2010153393 two-user license
  62.     hpplus06:~/tmp 9> cat clock.c
  63.     #include <stdio.h>
  64.     #include <time.h>
  65.  
  66.     int main(int argc, char *argv[])
  67.     {
  68.     clock_t s,f;
  69.  
  70.     s = clock();
  71.     while ((f = clock()) == s) ;
  72.     printf("%ld %f\n", (long)CLOCKS_PER_SEC, (double)(f-s) / CLOCKS_PER_SEC);
  73.     return 0;
  74.     } 
  75.     hpplus06:~/tmp 10> cc clock.c
  76.     hpplus06:~/tmp 11> ./a.out 
  77.     1000000 0.010000
  78.  
  79. Dan
  80. --
  81. Dan Pop
  82. CERN, CN Division
  83. Email: danpop@mail.cern.ch 
  84. Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
  85.